

import MainTableOperator from '../components/MainRowOperator';
import moment from 'moment';

import Common from '../../../public/actions/employing/common';
import ProcessTmpAction from '../../../public/actions/employing/processTemplate';

export default class ProcessTmp extends Common {
    constructor(comp) {
        super();
        this.comp = comp;
    }

    extend = [ProcessTmpAction]

    // 处理模版数据
    processTemplate = (template) => {
        let _this = this;
        const {props, action} = this.comp;
        const {
            emp,
            button
        } = props;
        let initValue = {};
        // 给每个字段添加查询对象
        let formList = [];
        Object.keys(template).forEach((item) => {
            if(template[item].moduletype === 'form') {
                formList.push(item);
            }
        });
        formList.forEach((item) => {
            template[item].items.forEach((item) => {
                if(!item.queryCondition) {
                    item.queryCondition = {};
                }
                if(item.initialvalue) {
                    initValue[item.attrcode] = item.initialvalue;
                }
            });
        });

        this.update({
            initValue: initValue
        });
        /*
        
                    <MainTableOperator
                        language={emp.language}
                        onClick={action.tableAct.rowClickHandle}
                        record={record}
                        index={index}
                        button={button}
                    /> */
        // 给主页面表格每行添加操作按钮
        template['list'].items.push({
            itemtype: 'customer',
            label: emp.language['gx6008-000046'],/* 国际化处理： 操作*/
            visible: true,
            fixed: 'right',
            width: '150px',
            attrcode: 'operation',
            render: (text, record, index) => {
                let billState = record.values['workflow_state'].value;
                return (
                    <div className="currency-opr-col">
                        {props.button.createOprationButton(
                            ['-1'].includes(billState)?
                            ['ta_edit', 'ta_delete','approveinfo','file_manager']:
                            ['approveinfo','file_manager'],
                            {
                                area: 'body',
                                // buttonLimit :2,
                                onButtonClick: (props, id, e) => {
                                    _this.comp.action.tableAct.rowClickHandle(id, record, index)
                                    // _this.comp.action.btnAct.headerClick(props, id, 'body', record)
                                },
                                popContainer: document.querySelector('.header2')
                            }
                        )}
                    </div>
                )
            }
        });

        template['list'].items.forEach((item) => {
            if(item.attrcode === 'approve_state') {
                item.itemtype = 'input';
            }
        });

        // 给批量添加的查询弹窗的申请日期添加默认值
        template['query'].items.forEach((item) => {
            if(item.attrcode === 'apply_date' && !item.initialvalue) {
                let firstValue = moment().date(1).format('YYYY-MM-DD');
                let secondValue = moment().format('YYYY-MM-DD');
                
                item.initialvalue = {
                    display: `${firstValue},${secondValue}`,
                    value: `${firstValue},${secondValue}`
                };
            }
        });

        template['batchaddmultiadd'].items.forEach((item) => {
            if(item.attrcode === 'hi_psnjob.pk_org' || item.attrcode === 'hi_psnjob.pk_org.code') {
                if(!item.queryCondition) {
                    item.queryCondition = {};
                }
                Object.assign(item.queryCondition, {
                    TreeRefActionExt: 'nccloud.web.hrhi.pub.sqlbuilder.OrgRefSqlBuilder'
                });
            }
        })

        return template
    }
    
}